package com.kayang.dd.controller;


import com.kayang.common.HttpMsg;
import com.kayang.common.exception.ParamException;
import com.kayang.dd.config.DDconfig;
import com.kayang.dd.service.AccessTokenService;
import com.kayang.dd.pojo.DDcontext;
import com.kayang.dd.pojo.UserModel;
import com.kayang.dd.pojo.param.ListUserByDeptParam;
import com.kayang.dd.service.UserService;
import com.kayang.nadapt.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController{
    @Autowired
    AccessTokenService tokenService;

    @Autowired
    DDconfig dDconfig;

    @Autowired
    UserService userService;

    @PostMapping("/create")
    public ResponseEntity creatUser(@Valid @RequestBody UserModel userModel){
        if(StringUtil.isNullOrEmpty(userModel.getName())){
            throw new ParamException("name不能为空");
        }
        if(StringUtil.isNullOrEmpty(userModel.getDept_id_list())){
            throw new ParamException("dept_id_list不能为空");
        }
        String url = dDconfig.getPath() + "/user/create?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(userModel.toMap(userModel));
        Map ret = userService.creatUser(userModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateUser(@Valid  @RequestBody UserModel userModel){
        if(StringUtil.isNullOrEmpty(userModel.getUserid())){
            throw new ParamException("userid不能为空");
        }
        String url = dDconfig.getPath() + "/user/update?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(userModel.toMap(userModel));
        Map ret = userService.updateUser(userModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("更新成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    /*
    *param:
    * {
        "userid":"user123"
       }
    *
    * */
    @PostMapping ("/delete")
    public ResponseEntity deleteUser(@RequestBody Map param){
        String url = dDconfig.getPath() + "/user/update?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map ret = userService.deleteUser(param, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }





    /**
     * param :
     *{
     *         "language":"zh_CN",
     *         "userid":"zhangsan"
     * }
     *
     * */
    @PostMapping("/get")
    public ResponseEntity getUser(@RequestBody Map param){
        String url = dDconfig.getPath() + "/user/get?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map ret = userService.getUser(param, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    @PostMapping("/listByDept")
    public ResponseEntity getUserByDept(@Valid @RequestBody ListUserByDeptParam deptParam){
        String url = dDconfig.getPath() + "/user/list?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(deptParam.toMap(deptParam));
        Map ret = userService.getUserByDept(deptParam, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }
}
